package cn.study.luckyapp.activityrule.command;

import cm.study.config.exception.LdException;
import cn.study.luckyapp.assembler.ActivityRuleAssembler;
import cn.study.luckyclient.dto.ActivityRuleAddCmd;
import cn.study.luckyclient.dto.data.ActivityRuleVo;
import cn.study.luckydomain.activityrule.ActivityRuleEntity;
import cn.study.luckydomain.gateway.ActivityRuleGateway;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@AllArgsConstructor
public class ActivityRuleAddCmdExe {
    private final ActivityRuleGateway activityRuleGateway;
    public ActivityRuleVo execute(ActivityRuleAddCmd cmd){
        ActivityRuleEntity add = activityRuleGateway.add(ActivityRuleAssembler.toAddEntity(cmd));
        return ActivityRuleAssembler.toActivityRuleVO(add);
    }
    public List<ActivityRuleVo> execute(List<ActivityRuleAddCmd> cmd){
        if(Objects.isNull(cmd)){
            throw new LdException("数据错误");
        }
        ArrayList<ActivityRuleVo> activityRuleVos = new ArrayList<>();
        for(ActivityRuleAddCmd activityRuleAddCmd:cmd){
            ActivityRuleVo execute = execute(activityRuleAddCmd);
            activityRuleVos.add(execute);
        }
        return activityRuleVos;
    }
}
